package com.ynding.cloud.auth.server.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ynding.cloud.auth.api.admin.common.util.BeanConvertUtils;
import com.ynding.cloud.auth.api.admin.domain.MenuDO;
import com.ynding.cloud.auth.api.admin.entity.Menu;
import com.ynding.cloud.auth.api.admin.entity.MenuRole;
import com.ynding.cloud.auth.api.admin.entity.Role;
import com.ynding.cloud.auth.server.admin.dao.MenuMapper;
import com.ynding.cloud.auth.server.admin.dao.MenuRoleMapper;
import com.ynding.cloud.auth.server.admin.dao.RoleMapper;
import com.ynding.cloud.auth.server.admin.service.IMenuService;
import com.ynding.cloud.common.model.bo.Query;
import com.ynding.cloud.common.model.util.BeanDtoVoUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p> </p>
 *
 * @author ynding
 * @version 2021/11/1
 **/
@Service
@Slf4j
@RequiredArgsConstructor
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu>
        implements IMenuService {

    private final MenuMapper menuMapper;
    private final MenuRoleMapper menuRoleMapper;
    private final RoleMapper roleMapper;

    @Override
    public List<MenuDO> findAll() {
        List<Menu> menus = menuMapper.selectList(new LambdaQueryWrapper<Menu>());
        return toMenuDOList(menus);
    }

    @Override
    public List<MenuDO> findList(Query query) {
        List<Menu> menus = menuMapper.selectList(new LambdaQueryWrapper<Menu>());
        return toMenuDOList(menus);
    }

    @Override
    public IPage<MenuDO> pageList(Query query) {
        IPage<Menu> menuPage = new Page<>(query.getPage(), query.getLimit());
        menuPage = menuMapper.selectPage(menuPage, null);
        IPage<MenuDO> page = BeanConvertUtils.toPageDO(menuPage, MenuDO.class);
        return page;
    }

    @Override
    public List<MenuDO> listByRid(Long roleId) {
        List<MenuRole> menuRoles = menuRoleMapper.selectList(new LambdaQueryWrapper<MenuRole>()
                .eq(MenuRole::getRid, roleId));
        List<Menu> menus = menuMapper.selectBatchIds(menuRoles.stream().map(MenuRole::getMid).collect(Collectors.toList()));
        return toMenuDOList(menus);
    }


    /**
     * 根据菜单id 查找角色
     *
     * @param menuId
     * @return
     */
    private List<Role> listRoleByMid(Long menuId) {
        List<MenuRole> menuRoles = menuRoleMapper.selectList(new LambdaQueryWrapper<MenuRole>()
                .eq(MenuRole::getMid, menuId));
        List<Role> roles = roleMapper.selectBatchIds(menuRoles.stream().map(MenuRole::getRid).collect(Collectors.toList()));
        return roles;
    }

    /**
     * 转换成领域类
     *
     * @param menus
     * @return
     */
    private List<MenuDO> toMenuDOList(List<Menu> menus) {
        List<MenuDO> list = BeanDtoVoUtils.toDtoList(menus, MenuDO.class);
        for (MenuDO menu : list) {
            List<Role> roles = listRoleByMid(menu.getId());
            menu.setRoles(roles);
        }
        return list;
    }
}
